package org.jeecg.modules.yw.job;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.yw.entity.Attendance;
import org.jeecg.modules.yw.entity.Attendapprove;
import org.jeecg.modules.yw.entity.EnterPrise;
import org.jeecg.modules.yw.mapper.AppointmentMapper;
import org.jeecg.modules.yw.service.EnterPriseService;
import org.jeecg.modules.yw.service.IAttendanceService;
import org.jeecg.modules.yw.service.IAttendapproveService;
import org.jeecg.modules.yw.service.MessagesService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class InitAttendanceJob implements Job {
    @Resource
    private EnterPriseService enterPriseService;
    @Resource
    private IAttendanceService attendanceService;
    @Resource
    private IAttendapproveService attendapproveService;
    @Resource
    private AppointmentMapper appointmentMapper;
    
    @Resource
    private MessagesService messageService;
    
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format(" yw 发送任务 InitAttendanceJob !  时间:" + DateUtils.getTimestamp()));
        QueryWrapper<Attendapprove> queryWra2=new QueryWrapper<>();
        queryWra2.eq("approve","1");
        queryWra2.le("startTime",DateUtils.getDate());
        queryWra2.ge("stopTime",DateUtils.getDate());
        List<Attendapprove> list2=attendapproveService.list(queryWra2);
        List<Attendance> list1=new ArrayList<>();
        for (int i = 0; i < list2.size(); i++) {
            Attendapprove attendapprove=list2.get(i);
            Attendance attendance=new Attendance();
            attendance.setAttenddate(DateUtils.getDate());
            attendance.setAttendtype(Integer.valueOf(attendapprove.getType()));
            attendance.setUserid(attendapprove.getWorkid());
            attendance.setCreatetime(DateUtils.getDate());
            list1.add(attendance);
        }
        // 清空工人池
        appointmentMapper.clearWorkers(org.jeecg.modules.yw.utils.DateUtils.getYesterDateStr());        
        attendanceService.saveBatch(list1);
        messageService.jsAutoConfirm();
        log.info(String.format(" yw 任务 InitAttendanceJob !  完成时间:" + DateUtils.getTimestamp()));
    }
}
